import ArrayList from '@ohos.util.ArrayList';
import { ReaderIn } from '../ReaderIn';
import { TypeConstants } from '../TypeConstants';
import { ObjectUtils } from '../utils/ObjectUtils';
import { WriterOut } from '../WriterOut';
import { ITypeAdapter } from './ITypeAdapter';

/**
 * @author tanwei
 * @description 数组列表转换适配器
 */
export class ArrayListTypeAdapter implements ITypeAdapter {
  write(writerOut: WriterOut): WriterOut {
    //获取列表源
    let arrayListTarget=writerOut.getTarget() as ArrayList<ESObject>
    //数组列表转换数组
    writerOut.setWriteValue(ObjectUtils.copyArrayListToArray(
      arrayListTarget
    ))
    return writerOut
  }

  read(readerIn: ReaderIn): ReaderIn {
    let arrayListTarget = readerIn.getTarget() as ArrayList<ESObject>
    let arrTarget = ObjectUtils.copyArrayListToArray(
      arrayListTarget
    )
    let type = readerIn.getTypeFactory().create(arrTarget)
    let arrayListReaderIn = new ReaderIn(
      readerIn.getJson(), arrTarget, readerIn.getTypeFactory(),readerIn.getReadNums()
    )
    arrayListReaderIn.setDefault(readerIn.getDefault())
    return type.read(arrayListReaderIn)
  }

  typeConstants(): TypeConstants {
    return TypeConstants.TYPE_ARRAY_LIST
  }
}